#include "Query.h"


using namespace std;

Query::Query() {
    num_of_query = 0;
};

Query::Query(const Query&) {};
Query::~Query() {};

void Query::setLine(string s) {
    line = s;
}

string Query::getLine() {
    return line;
}

void Query::printParam() {
    for (list<string>::iterator print = temp.begin();print!=temp.end();print++) {
        cout << *print << endl;
    }

    for (list<string>::iterator print = query_name.begin();print!=query_name.end();print++) {
        cout << *print << endl;
    }

    cout << "Jumlah query = " << temp.size() << endl;
}

void Query::replace(string s1, string s2) {
    int i,j;
    string _temp;
    int len = s1.length();
    int pass;
    

    for (list<string>::iterator it = temp.begin();it!=temp.end();it++) {

        for (pass=0;pass+len <= (*it).length();pass++) {

            if (s1.compare((*it).substr(pass,len)) == 0) {
                _temp.append(s2);
                pass += (len-1);
            } else {
                _temp.push_back((*it)[pass]);
                
            }
        }

        if (pass!=(*it).length()) {
            while (pass<=(*it).length()) {
             _temp.push_back((*it)[pass]);
             pass++;
             
            }
        }

        (*it) = _temp;
        _temp.clear();
    }
}

void Query::Parse() {
    int i,j,k;
    string _temp;

    for(i=0;i<line.length();i++) {
        if (line[i]=='(') {
            query_name.push_back(_temp);
        }
        
        if (line[i]==',') {
            if (line[j]==')') {
                temp.push_back(_temp);
                //cout << "got 1 query" << endl;
                _temp.clear();
                i++;
            }
        }

        if (line[i]=='.') {
            temp.push_back(_temp);
            //cout << "got 1 query" << endl;
            _temp.clear();
        }

        _temp.push_back(line[i]);

        j = i;
    }

}

list<Transisi*>::iterator Query::getTransition() {
	list<Transisi*>::iterator it = trans.begin();
	for (int i = 0; i < temp.length; i++) {
		it++;
	}
	return it;
}
